"""
考勤记录模型
"""
from datetime import datetime
from app.extensions import db


class Attendance(db.Model):
    """考勤记录表"""
    id = db.Column(db.Integer, primary_key=True)
    staff_name = db.Column(db.String(100), nullable=False, comment='人员姓名')
    project_name = db.Column(db.String(100), comment='所属项目')
    event_type = db.Column(db.String(10), nullable=False, comment='事件类型：进门/出门')
    event_time = db.Column(db.DateTime, nullable=False, comment='事件时间')
    is_resident = db.Column(db.Boolean, comment='是否驻公司')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 为兼容性添加的字段
    staff_id = db.Column(db.Integer, db.ForeignKey('staff.id'), comment='员工ID')
    date = db.Column(db.Date, comment='日期')
    check_in_time = db.Column(db.Time, comment='上班时间')
    check_out_time = db.Column(db.Time, comment='下班时间')
    work_hours = db.Column(db.Float, comment='工作时长(小时)')
    is_late = db.Column(db.Boolean, default=False, comment='是否迟到')
    is_early_leave = db.Column(db.Boolean, default=False, comment='是否早退')
    status = db.Column(db.String(20), default='正常', comment='考勤状态')
    notes = db.Column(db.Text, comment='备注')
    
    # 关系
    staff = db.relationship('Staff', backref=db.backref('attendance_records', lazy=True))
    
    def __repr__(self):
        return f'<Attendance {self.staff_name}-{self.event_type}>'
